package controller

import (
	"fileshare/controller/server"
	"fileshare/controller/tools"
	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
	"net/http"
)

/**
* @program: fileshare
*
* @description:
*
* @author: Koco.Allien
*
* @create: 2020-04-05 17:40
**/
func LoginController(c *gin.Context) {
	data, _, ok := tools.StartDate(c, "登录", 0)
	if ok {
		IndexController(c)
	} else {
		//没有登录
		c.HTML(http.StatusOK, "login.html", data)
	}
}

//登录信息 获取传入的参数信息 account passwd
func LoginControllerPost(c *gin.Context) {
	data, _, ok := tools.StartDate(c, "登录", 0)
	if ok {
		IndexController(c)
	} else {
		//没有登录
		account := c.PostForm("account")
		passwd := c.PostForm("passwd")
		if ok, user := server.ISExist(account, passwd); ok && account != "" {
			//配置session
			session := sessions.Default(c)
			session.Set("user", user)
			session.Save()
			IndexController(c)
		} else {
			//不存在 返回登陆页面
			data["error"] = "密码或者账户错误"
			data["role"] = 0
			c.HTML(http.StatusOK, "login.html", data)
		}
	}
}

func LoginControllerToOut(c *gin.Context) {
	_, _, ok := tools.StartDate(c, "", 1)
	if ok {
		session := sessions.Default(c)
		session.Delete("user")
		session.Clear()
		session.Save()
	}
	IndexController(c)
}
